﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/2 12:09:39
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using System.Net.Mail;
    using System.Data;
    using System.Xml;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 邮件。
    /// </summary>
    public class Mail : MailMessage
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.Mail"/>。
        /// </summary>
        public Mail() { }
        
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.Mail"/>。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        public Mail(IDataReader reader)
        {
            this.Body = reader.GetString("Body");
            this.Subject = reader.GetString("Subject");
            string displayName = reader.GetString("DisplayName");
            if (!string.IsNullOrEmpty(displayName))
                this.From = new MailAddress(reader.GetString("WebMail"), displayName);
            else
                base.From = new MailAddress(reader.GetString("WebMail"));
            this.ToAddresses = reader.GetString("ToAddresses");
            foreach (string to in ToAddresses.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                this.To.Add(new MailAddress(to));
            }
            this.IsBodyHtml = reader.GetBoolean("IsBodyHtml");
            this.BodyEncoding = reader.GetEncoding("BodyEncoding");
            this.SubjectEncoding = reader.GetEncoding("SubjectEncoding");
            this.Priority = reader.TGet<MailPriority>("Priority", MailPriority.Normal);
        }

        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.Mail"/>。
        /// </summary>
        /// <param name="xml">XML字符串节点。</param>
        public Mail(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlNode node = doc.SelectSingleNode("email");
            this.Priority = node.GetAttribute("priority", null).ToEnum<MailPriority>(MailPriority.Normal);
            string htmlBody = null, textBody = null;
            this.IsBodyHtml = true;
            foreach (XmlNode nd in node)
            {
                if (nd.NodeType == XmlNodeType.Document)
                    continue;
                switch (nd.Name)
                {
                    case "subject":
                        this.Subject = nd.InnerText;
                        break;
                    case "body":
                        this.Body = nd.InnerText;
                        break;
                    case "textbody":
                        textBody = nd.InnerText;
                        break;
                    case "htmlbody":
                        htmlBody = nd.InnerText;
                        break;
                }
            }
            if (!string.IsNullOrEmpty(htmlBody))
            {
                this.Body = htmlBody;
            }
            else if (!string.IsNullOrEmpty(textBody))
            {
                this.Body = textBody;
                this.IsBodyHtml = false;
            }
            this.BodyEncoding = SiteSettings.Current.BodyEncoding.ToEncoding();
            this.SubjectEncoding = SiteSettings.Current.SubjectEncoding.ToEncoding();
            this.From = new MailAddress(SiteSettings.Current.WebMail, SiteSettings.Current.DisplayName);
        }
        /// <summary>
        /// 获取或设置发送的电子邮件字符串，以“,”隔开。
        /// </summary>
        public string ToAddresses { get; set; }

        /// <summary>
        /// 将电子邮件插入到队列中。
        /// </summary>
        public void Queue()
        {
            CommonDataProvider.Current.QueueMail(this);
        }
        /// <summary>
        /// 发送电子邮件。
        /// </summary>
        /// <returns>如果发送成功返回<c>true</c>，否则返回<c>false</c>。</returns>
        public bool Send()
        {
            this.Body = this.Body.Replace("\r", string.Empty);
            this.Body = this.Body.Replace("\n", "\r\n");
            try
            {
                Mails.GetClient().Send(this);
                return true;
            }
            catch (Exception ex)
            {
                Logs.Error("邮件发送错误：", "Mail Sender", EventID.Mail, ex, "电子邮件");
                return false;
            }
        }
    }
}